package net.edu_soft.ec.asset.service.impl;

import net.edu_soft.ec.asset.mapper.RolePermissionMapper;
import net.edu_soft.ec.asset.pojo.Permission;
import net.edu_soft.ec.asset.pojo.Role;
import net.edu_soft.ec.asset.pojo.RolePermission;
import net.edu_soft.ec.asset.pojo.RolePermissionExample;
import net.edu_soft.ec.asset.service.PermissionService;
import net.edu_soft.ec.asset.service.RolePermissionService;
import net.edu_soft.ec.asset.service.RoleService;
import net.edu_soft.ec.common.exception.ParameterException;
import net.edu_soft.ec.common.util.PuingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 注入权限业务
     */
    @Autowired
    private PermissionService permissionService;

    /**
     * 注入角色业务
     */
    @Autowired
    private RoleService roleService;

    @Override
    public int save(Integer roleId, String permissionIds) {
        int i = 0;
        Assert.hasLength(permissionIds, "为角色添加权限失败，权限id串为空");
        PuingUtil.notNullByZero(roleId, "为角色添加权限失败，角色id为空或小于等于0");
        //查询角色
        Role role = roleService.find(roleId);
        if (role == null) {
            throw new ParameterException("为角色添加权限失败，角色不存在 id：" + roleId);
        }
        //提取权限id
        List<Integer> ids = PuingUtil.getNumber(permissionIds);
        if (ids.size() > 0) {
            //遍历查询是否存在该权限
            for (Integer permissionId : ids) {
                Permission permission = permissionService.find(permissionId);
                if (permission == null) {
                    throw new ParameterException("为角色添加权限失败，权限不存在 id：" + permissionId);
                }
                //添加到数据库
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(permissionId);
                i = rolePermissionMapper.insertSelective(rolePermission);

            }
        }
        return i;
    }

    @Override
    public int del(Integer roleId, String permissionIds, Integer isDel) {
        int i = 0;
        if (isDel == 1 && isDel != null) {
            Assert.hasLength(permissionIds, "为角色删除权限失败，权限id串为空");
            PuingUtil.notNullByZero(roleId, "为角色删除权限失败，角色id为空或小于等于0");
            //提取权限id
            List<Integer> ids = PuingUtil.getNumber(permissionIds);
            if (ids.size() > 0) {
                //遍历查询是否存在该权限关系
                for (Integer permissionId : ids) {
                    RolePermission rolePermission = sel(roleId, permissionId);
                    if (rolePermission == null) {
                        throw new ParameterException("为角色删除权限失败，" +
                                "角色权限关系不存在 permissionId：" + permissionId + ", roleId: " + roleId);
                    }
                    //删除
                    i = rolePermissionMapper.deleteByPrimaryKey(rolePermission.getRolePmsId());
                }
            }
        }
        return i;
    }

    @Override
    public List<Integer> selPermissionIds(Integer id) {
        List<Integer> permissionIds = new ArrayList<>();
        PuingUtil.notNullByZero(id,"角色权限关系，查询失败，id为空或者小于等于0");
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        //添加角色id
        criteria.andRoleIdEqualTo(id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(example);
        if(rolePermissions.size()>0){
            //提取id
            for (RolePermission rp:rolePermissions) {
                permissionIds.add(rp.getPermissionId());
            }
            return permissionIds;
        }
        return null;
    }

    /***
     * 查询该关系是否存在
     * @param roleId 角色id
     * @param permissionId 权限id
     * @return
     */
    private RolePermission sel(Integer roleId, Integer permissionId) {
        PuingUtil.notNullByZero(roleId, "为角色添加权限失败，角色id为空或小于等于0");
        PuingUtil.notNullByZero(permissionId, "为角色添加权限失败，权限id为空或小于等于0");
        RolePermissionExample example = new RolePermissionExample();
        RolePermissionExample.Criteria criteria = example.createCriteria();
        //添加角色id和权限id
        criteria.andRoleIdEqualTo(roleId);
        criteria.andPermissionIdEqualTo(permissionId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(example);
        if (rolePermissions.size() > 0) {
            return rolePermissions.get(0);
        }
        return null;
    }

}
